package org.king.mapper;

import org.apache.ibatis.annotations.*;
import org.king.pojo.entity.Account;

import java.util.List;

@Mapper
public interface AccountMapper {

    @Select("select * from user")
    public List<Account> getAllUsers();

    @Select("select * from user where id = #{id}")
    public Account getUserById(@Param("id") int id);

    @Select("select * from user where email=#{text} or username =#{text} limit 1")
    public Account getUserByEmailOrUsername(@Param("text") String text);

    @Select("select * from user where username=#{username}")
    public Account getUserByUsername(@Param("username") String username);

    @Select("select * from user where email=#{email}")
    public Account getUserByEmail(@Param("email") String email);

    @Insert("INSERT INTO user(username, password, email, role, register_time) " +
            "VALUES (#{username}, #{password}, #{email}, 'user', NOW())")
    public int insertUser(
            @Param("username") String username,
            @Param("password") String password,
            @Param("email") String email
    );

    @Update("update user set password=#{password} where email=#{email}")
    public int updatePassword(@Param("password") String password, @Param("email") String email);
}
